package com.situ.mvc.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.situ.mvc.common.ServerResponse;
import com.situ.mvc.entity.Student;
import com.situ.mvc.entity.StudentTeacherBean;
import com.situ.mvc.entity.Teacher;

@Controller
@RequestMapping("/student")
public class StudentController {

	@RequestMapping("/insert1")
	public static ModelAndView insert1(Student student) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("student",student);
		modelAndView.setViewName("/student_info.jsp");
		return modelAndView;
	}
	
	@RequestMapping("getStudentInsert")
	public static String getStudentInsert() {
		return"student_insert";
	}
	@RequestMapping("getStudentTeacherInsert")
	public static String getStudentTeacherInsert() {
		return"student_teacher_insert";
	}
	
	@RequestMapping(value = "/insert",method = RequestMethod.POST)
	public static String insert(Student student,Model model) {
		model.addAttribute("student",student);
		return "student_info";
	}
	
	@RequestMapping("/insertStudentAndTeacher")
	public String insertStudentAndTeacher(StudentTeacherBean bean,Model model) {
		System.out.println(bean.getStudent());
		Student student = bean.getStudent();
		Teacher teacher = bean.getTeacher();
		System.out.println(bean.getTeacher());
		model.addAttribute("student",student);
		model.addAttribute("teacher",teacher);
		return "student_info";
	}
	
	@RequestMapping("/deleteById")
	public void deleteById() {
		System.out.println("StudentController.deleteById()");
	}
	@RequestMapping("/pageList")
	@ResponseBody
	public List<Student> pageList() {
		System.out.println("StudentController.pageList()");
		List<Student> list = new ArrayList<Student>();
		Student student1 = new Student(1, "张三1", 23, "男");
		Student student2 = new Student(1, "张三2", 23, "男");
		Student student3 = new Student(1, "张三3", 23, "男");
		Student student4 = new Student(1, "张三4", 23, "男");
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.add(student4);
		
		return list;
	}
	
	@RequestMapping("/pageObj")
	@ResponseBody
	public ServerResponse pageObj() {
		System.out.println("StudentController.pageList()");
		List<Student> list = new ArrayList<Student>();
		Student student1 = new Student(1, "张三1", 23, "男");
		Student student2 = new Student(1, "张三2", 23, "男");
		Student student3 = new Student(1, "张三3", 23, "男");
		Student student4 = new Student(1, "张三4", 23, "男");
		list.add(student1);
		list.add(student2);
		list.add(student3);
		list.add(student4);
		
		ServerResponse serverResponse = ServerResponse.createSuccess("成功", list);
		
		return serverResponse;
	}
	@RequestMapping("/deleteAll") 
	public void deleteAll(Integer[] ids,HttpServletRequest request) {
		System.out.println("StudentController.deleteAll()");
		String[] idArray = request.getParameterValues("ids");
		System.out.println(Arrays.toString(idArray));// [1, 2, 3] 
		System.out.println(Arrays.toString(ids));// [1, 2, 3] 
	}
	@RequestMapping("/deleteById")
	public String deleteById(Integer id) {
		System.out.println("StudentController.deleteById()");
		System.out.println(id);
		
		// 转发到某个jsp界面
		//return "student_info";
		
		// 转发到/student/pageList.action
		//return "forward:/student/pageList.action";
		
		// 重定向
		return "redirect:/student/pageList.action";
	}
}
